import Ember from 'ember';
import Changeset from 'ember-changeset';
import ConsultValidations from '../../../validations/consult';
import lookupValidator from 'ember-changeset-validations';

export default Ember.Controller.extend(ConsultValidations, {
    constants: Constants,
    detailEdit: false,
    delFlag: true,
    dateService: Ember.inject.service("date-service"),
    store: Ember.inject.service("store"),
    dataLoader: Ember.inject.service("data-loader"),
    consultObs: function() {
        var model = this.get("consult");
        console.log("model consult", model);
        if (!model) {
            return null;
        }
        var consultModel = new Changeset(model, lookupValidator(ConsultValidations), ConsultValidations);
        this.set("consultModel", consultModel);
    }.observes("consult"),
    refreshStaffList: function() {
        var route = App.lookup('route:business.mainpage.consultation-management');
        App.lookup('controller:business.mainpage').refreshPage(route);
    },
    abd: function() {},
    defaultStaff: Ember.computed('consult.receiveStaff', 'staffListFirst', function() {
        return this.get('consult.receiveStaff');
    }),
    //预警时间验证
    // warningTimeValidate() {
    //     var callTime = this.get('warning.callTime');
    //     if (!callTime || callTime.length === 0) {
    //         this.set("warning.page_errors.title", "预警时间不能为空");
    //         this.get("warning").incrementProperty("page_errorsCnt");
    //         return false;
    //     } else {
    //         this.set("warning.page_errors.title", null);
    //     }
    //     return true;
    // },
    actions: {
        invalid() {
            //alert("invalid");
        },
        //编辑按钮
        detailEditClick: function(consult) {
            this.set('detailEdit', true);
        },
        //取消按钮
        detailCancel: function() {
            var id = this.get('id');
            var editMode = this.get('editMode');
            var mainpageController = App.lookup('controller:business.mainpage');
            this.set('detailEdit', false);
            //alert("详情");
            if (id && editMode == 'edit') {
                this.get("consult").rollbackAttributes();
                this.set("consultModel", new Changeset(this.get("consult"), lookupValidator(ConsultValidations), ConsultValidations));
            } else {
                mainpageController.switchMainPage('consultation-management', {});
            }
        },
        //存储
        saveWarning() {
            //alert("保存");
            console.log("++++++++++++delFlag+++++++++++++", this.get("delFlag"));
            if (this.get("delFlag")) {
                //alert("保存进来了");
                var _self = this;
                var consultModel = this.get("consultModel");
                var mainpageController = App.lookup('controller:business.mainpage');
                var editMode = this.get('editMode');
                var id = this.get('id');
                let statusInfo = this.get("dataLoader").findDict(Constants.consultStatus1);
                console.log("statusInfo is",statusInfo);
                consultModel.validate().then(function() {
                    //alert("save   out");
                    if (consultModel.get('errors.length') === 0) {
                      consultModel.set("consultStatus",statusInfo);
                        App.lookup('controller:business.mainpage').openPopTip("正在保存");
                        consultModel.save().then(function() {
                            App.lookup('controller:business.mainpage').showPopTip("保存成功");
                            if (id && editMode == 'edit') {
                                console.log("id is", id);
                                console.log("editMode is", editMode);
                                _self.set('detailEdit', false);
                            } else {
                                mainpageController.switchMainPage('consultation-management');
                                _self.set('detailEdit', false);
                            }
                        });
                    } else {
                        consultModel.set("validFlag", Math.random());
                    }
                });
            }
        },
        //删除按钮
        delById: function(consult) {
            //this.set('showpopInvitePassModal', true);
            this.set("delFlag", false);
            var _self = this;
            App.lookup('controller:business.mainpage').showConfirm("是否确定删除此床位记录", function() {
                App.lookup('controller:business.mainpage').openPopTip("正在删除");
                consult.set("delStatus", 1);
                consult.save().then(function() {
                    App.lookup('controller:business.mainpage').showPopTip("删除成功");
                    _self.set("delFlag", true);
                    //console.log("delFlag删除", _self.get("delFlag"));
                    var mainpageController = App.lookup('controller:business.mainpage');
                    mainpageController.switchMainPage('consultation-management');
                });
            });
        },
        dpShowAction(e) {
        },
        //接待人
        selectStaff(staff) {
            this.set("staff", staff);
            this.get("consult").set("receiveStaff", staff);
        },
        //性别字典
        sexSelect: function(sexDict) {
            console.log("sexDict in", sexDict);
            this.get("consult").set("advGender", sexDict);
        },
        //与老人关系字典
        relationSelect: function(relationDict) {
            this.get("consult").set("consultRelation", relationDict);
        },
        //咨询方式
        channelSelect:function(channelDict){
          this.get("consult").set("consultChannel", channelDict);
        },
        //了解渠道
        sourceSelect:function(sourceDict){
          this.get("consult").set("advWay", sourceDict);
        },
        //老人性别
        customerSexSelect: function(customerSexDict) {
            console.log("customerSexDict in", customerSexDict);
            this.get("consult").set("customerGender", customerSexDict);
        },
        //咨询时间
        changeADVDateAction(date) {
            console.log("date is who", date);
            var stamp = this.get("dateService").timeToTimestamp(date);
            this.set("consultModel.advDate", stamp);
        },
        //预约参观日期
        changeAppointmentAction(date){
          var stamp = this.get("dateService").timeToTimestamp(date);
          this.set("consultModel.appointmentDate", stamp);
        },
        //老人生日
        changeBrithAction(date){
          var stamp = this.get("dateService").timeToTimestamp(date);
          this.set("consultModel.customerBrith", stamp);
        },
        //老人自理能力
        abilitySelect: function(abilityDict) {
            this.get("consultModel").set("customerSelfCareAbility", abilityDict);
        },
        //老人文化程度
        educationSelect: function(educationDict) {
            this.get("consultModel").set("customerEducation", educationDict);
        },
    }
});
